package com.czd.support.controller;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.base.utils.WebUtils;
import com.czd.support.common.Configuration;
import com.czd.support.model.Manager;
import com.czd.support.model.Order;
import com.czd.support.service.OrderService;
import com.czd.support.util.SMSUtil;

/**
 * @Description  TODO
 * 
 * @Author Jinma.Xiao
 * @Date    2015年7月28日 下午2:43:28
 * @Version v1.0
 */
 
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Autowired
	private OrderService orderService;

	/**
	 * 列表页
	 * 
	 * @param model
	 * @param request
	 * @param answers
	 * @return
	 */
	@RequestMapping(value = { "/", "", "list" }, method = RequestMethod.GET)
	public String list(Order order, HttpServletRequest request, Model model,
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		Manager manager = this.getSesionUser(request);
		if (manager != null) {
			order.setPageSize(Configuration.getInstance()
					.getDefault_page_size());
			order.setPageNo(pageNo);
			String kw = WebUtils.findParamStr(request, "kw");
			int deliver = WebUtils.findParamInt(request, "deliver");
			System.out.println(kw);
			order.setIsDeliver(deliver);
			Paginate paginate = orderService.queryForPage(order, kw);
			model.addAttribute("deliver", deliver);
			model.addAttribute("paginate", paginate);
			return "order/list";
		} else {
			return "redirect:/login";
		}

	}

	/**
	 * 明细 查看
	 * 
	 * @param id
	 * @param isAnswer
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		Order order = orderService.findDetail(id);
		model.addAttribute("order", order);
		return "order/details";
	}
	
	/**
	 * 发货
	 * 
	 * @param id
	 * @param isAnswer
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String update(@PathVariable int id, Model model,Order order) {
		order.setId(id); 
		orderService.updateDeliver(order);
		return "redirect:/order/list";
	}
	/**
	 *  
	 * 
	 * @param id
	 * @param isAnswer
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/phoneSent", method = RequestMethod.POST)
	public String phoneSent( HttpServletRequest  request  , Model model,Order order) {
		String neirong = request.getParameter("neirong");
		String phone = request.getParameter("phone");
		String orderId = request.getParameter("orderId");
		try {
			String sendMessage = SMSUtil.sendMessage(phone, neirong);
			System.out.println(sendMessage);  
			order.setId(Integer.parseInt(orderId));   
			orderService.updateDeliver(order);
			model.addAttribute("msg", neirong);
			String  ss= "发货成功";
			model.addAttribute("yOn",ss );
			return  "order/show";
		} catch (Exception e) {  
			String  ss= "发货失败";
			model.addAttribute("msg", neirong);
			model.addAttribute("yOn", ss);
			e.printStackTrace();
			return  "order/show";
		}
	     
	}      
}
